package com.botann.projects.base;


import com.botann.projects.constants.Code;
import com.botann.projects.constants.Code;

/**
 * StorageManager
 * BaseController
 * Created by 刘山国 on 2017/5/19.
 * Copyright (c) 2017 刘山国. All rights reserved.
 */

public abstract class BaseController extends BaseImp {

    protected Model model(Integer code,String userInfo,Object content){
        if (content == null) content = "";
        if (userInfo == null) userInfo = "";
        return new Model(code,userInfo,content);
    }

    protected Model successModel() {
        return successModel("");
    }

    protected Model successModel(Object content){
        return successModel("",content);//1 在返回值中 return model(Code.SUCCESS,userInfo,content);
    }
    protected Model successModel(String userInfo,Object content){
        return model(Code.SUCCESS,userInfo,content);
    }

    protected Model failureModel(){
        return failureModel(Code.UN_IMPLEMENT,"接口还未实现!");
    }
    protected Model failureModel(String userInfo) {
        return failureModel(Code.ERROR,userInfo,"");
    }
    protected Model failureModel(Integer code) {
        return failureModel(code,"");
    }

    protected Model failureModel(Integer code,String userInfo){
        return failureModel(code,userInfo,"");
    }
    protected Model failureModel(Integer code,String userInfo,Object content) {
        return model(code,userInfo,content);
    }

    protected Model failureModel(Exception e){//1 在返回值中 设置了 userINFO =""
        return failureModel(Code.ERROR,e);
    }

    private Model failureModel(Integer defaultErrorCode,Exception e){
        e.printStackTrace();
        logger.error("Code:  "+defaultErrorCode+e);
        if (e instanceof SGRuntimeException) {// 1如果是运行时异常
            defaultErrorCode = ((SGRuntimeException) e).getErrorCode();
            return failureModel(defaultErrorCode, e.getMessage());
        } else {
            return failureModel(defaultErrorCode, "服务器错误!");
        }
//        String userInfo = StringUtils.isEmpty(e.getMessage())?e.toString():e.getMessage();
//        if (userInfo.startsWith("org.hibernate")) userInfo = "查询出错!";
//        return failureModel(defaultErrorCode, userInfo);
    }
}
